/**
*	插件：nwjs的
*	开发者：雨中磐石
*	地址：www.rockoa.com
*	日期：2023-07-17
*/

js.nwjs = {
	
	init:function(){
		
	},
	create:function(cans){
		for(var i in cans)js.nwjs[i]=cans[i];
		js.nwjs.load();
	},
	load:function(){
		this.win = nw.Window.get();
		var icon = 'image/logo.png';
		var tray = new nw.Tray({ title:this.systitle, icon: icon});
		tray.tooltip = this.systitle+'\n'+this.companyname+'-'+this.name+'';
		
		var menu = new nw.Menu();
		menu.append(new nw.MenuItem({label: '打开窗口',click:function(){
			js.nwjs.show();
		}}));
		menu.append(new nw.MenuItem({label: '取消闪烁',click:function(){
			js.nwjs.iconok();
			if(js.notify)js.notify.close();
		}}));
		menu.append(new nw.MenuItem({label: '退出',click:function(){
			js.nwjs.closebool = true;
			js.nwjs.exit();
		}}));
		tray.menu = menu;
		tray.on('click',function(){
			js.nwjs.show();
		});
		
		this.tray = tray;
		this.win.removeAllListeners('close');
		this.win.on('close',function(){
			if(js.nwjs.closebool){
				js.nwjs.exit();
			}else{
				js.nwjs.hide();
			}
		});
		js.reload = function(){js.nwjs.reload();}
	},
	unload:function(){
		if(!this.tray)return;
		this.win.removeAllListeners('close');
		this.tray.remove();
		this.tray = false;
	},
	hide:function(){
		this.win.hide();
	},
	show:function(){
		this.win.show();
		this.win.focus();
	},
	exit:function(){
		nw.App.quit();
	},
	jumpicon:function(oi){
		if(!this.tray)return;
		clearTimeout(this.jumptime);
		var s='image/logo.png';
		if(oi==1)s='image/logono.png';
		this.tray.icon = s;
		oi = (oi==1)?0:1;
		this.jumptime=setTimeout('js.nwjs.jumpicon('+oi+')',500);
	},
	iconok:function(){
		if(!this.tray)return;
		clearTimeout(this.jumptime);
		var s='image/logo.png';
		this.tray.icon = s;
	},
	getpath:function(){
		if(this.path)return this.path;
		if(!this.pathobj)this.pathobj = require('path');
		var oatg = this.pathobj.dirname(process.execPath);
		oatg	 = oatg.replace(/\\/g, '/');
		this.path= oatg;
		return oatg;
	},
	reload:function(){
		this.win.reloadIgnoringCache();
	},
	clipobj:function(){
		if(!this.clipobj1)this.clipobj1 = nw.Clipboard.get();
		return this.clipobj1;
	},
	clear:function(){
		nw.App.clearCache();
		localStorage.clear();
		var na = nw.App.manifest.name;
		js.modal.confirm('基本缓存已删除，更多缓存删除，是否打开对应['+na+']目录？需手动全部删除目录。',function(jg){
			if(jg=='yes'){
				var naea = nw.App.dataPath;
				var oru= naea.split(na)[0]+na;
				nw.Shell.openItem(oru);
				nw.App.quit();
			}
		});
	},
	openfile:function(path){
		var ss = this.getpath()+'/'+path;
		this.runcmd(ss);
	},
	runcmd:function(cmd){
		if(!this.execcmd)this.execcmd= require('child_process').exec;
		this.execcmd(cmd);
	}
}